<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="filter-item">
        <span class="span">服务名称 </span>
        <el-input v-model="listQuery.serviceName" placeholder="请输入服务名称" style="width: 200px" />
      </div>
      <div class="filter-item">
        <span class="span">状态 </span>

        <el-select v-model="listQuery.status" clearable placeholder="请选择状态" style="width: 200px">
          <el-option v-for="item in dictList('is_use')" :key="'type' + item.label" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <div class="filter-item">
        <span class="span">服务类型 </span>
        <el-select v-model="listQuery.serviceType" clearable placeholder="请选择服务类型" style="width: 200px">
          <el-option v-for="item in dictList('data_service_type')" :key="'type' + item.label" :label="item.label" :value="item.value" />
        </el-select>
      </div>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }}
      </el-button>
      <el-button v-waves plain v-permission="['marketservice:MarketServiceIntegration:add']" class="filter-item" style="margin-left: 10px" type="primary" icon="el-icon-edit" @click="handleCreate">
        {{ $t('table.add') }}
      </el-button>
      <el-button
        v-waves
        plain
        v-permission="['marketservice:MarketServiceIntegration:delete']"
        class="filter-item"
        style="margin-left: 10px"
        type="danger"
        icon="el-icon-delete"
        @click="handleBathDelete"
      >
        {{ $t('table.delete') }}
      </el-button>
      <!--      <el-button v-waves v-permission="['marketservice:MarketServiceIntegration:import']" class="filter-item" type="primary" icon="el-icon-upload2" @click="handleImport">-->
      <!--        {{ $t('table.import') }}-->
      <!--      </el-button>-->
      <!--      <el-button v-waves v-permission="['marketservice:MarketServiceIntegration:export']" :loading="downloadLoading" class="filter-item" type="primary" icon="el-icon-download" @click="handleExport">-->
      <!--        {{ $t('table.export') }}-->
      <!--      </el-button>-->
    </div>

    <el-table
      ref="table"
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      element-loading-text="正在加载，请稍后"
      border
      fit
      highlight-current-row
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="序号" align="center" type="index" width="100" />
      <el-table-column label="服务名称" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.serviceName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务编号" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.serviceNo }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.status | dictLabel('is_use') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" align="center" min-width="150px">
        <template slot-scope="{ row }">
          <span>{{ row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务类型" min-width="150px" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.serviceType | dictLabel('data_service_type') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed='right' align="center" min-width="230" class-name="small-padding fixed-width">
        <template slot-scope="{ row }">
          <!--          <el-button  size="small" type="text" icon="el-icon-video-play" @click="runJobNow(row)">执行一次</el-button>-->
          <el-button
            v-if="row.checkStatus == 0"
            v-permission="['task:schedule:job:change:job:status']"
            type="text"
            size="small"
            icon="el-icon-video-play"
            style="margin-left: 10px"
            @click="handleChangeJobStatus(row, '1', '启动')"
            >开始</el-button
          >
          <el-button
            v-if="row.checkStatus == 1"
            v-permission="['task:schedule:job:change:job:status']"
            type="text"
            size="small"
            icon="el-icon-video-pause"
            class="delete-text-btn"
            style="margin-left: 10px"
            @click="handleChangeJobStatus(row, '0', '停止')"
            >停止</el-button
          >
          <el-button v-permission="['marketservice:MarketServiceIntegration:update']" type="text" size="small" icon="el-icon-edit" @click="handleUpdate(row)">
            {{ $t('table.edit') }}
          </el-button>
          <el-button v-permission="['marketservice:MarketServiceIntegration:info']" type="text" size="small" icon="el-icon-tickets" @click="handleInfo(row)"> 详情 </el-button>
          <el-button v-permission="['marketservice:MarketServiceIntegration:delete']" type="text" size="small" icon="el-icon-delete" class="delete-text-btn" @click="handleDelete(row, 'deleted')">
            {{ $t('table.delete') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <import ref="import" template-url="/marketservice/MarketServiceIntegration/template" import-url="/marketservice/MarketServiceIntegration/import" @refreshList="getList" />
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" :page-sizes="pageArray" @pagination="getList" />

    <market-service-integration-form ref="form" @refreshList="getList" />
    <market-service-integration-info ref="info" @refreshList="getList" />
  </div>
</template>

<script>
import { deleteMarketServiceIntegration, fetchMarketServiceIntegrationList, exportMarketServiceIntegration, runJobNow } from '@/api/marketservice/marketServiceIntegrationTask'
import Import from '@/components/Import/import'
import waves from '@/directive/waves' // waves directive
import permission from '@/directive/permission/permission'
import marketServiceIntegrationForm from './marketServiceIntegrationTaskForm'
import marketServiceIntegrationInfo from './marketServiceIntegrationTaskInfo'
import Pagination from '@/components/Pagination'
import { startOneForSpark } from '@/api/dataextractiontask/dataExtractionTask'
import { changeJobStatus } from '@/api/job/metadataJob'

export default {
  name: 'MarketServiceIntegrationList',
  components: { marketServiceIntegrationForm, marketServiceIntegrationInfo, Pagination, Import },
  directives: { waves, permission },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      multipleSelection: [],
      downloadLoading: false,
      listLoading: true,
      pageArray: this.$store.getters.pageArray,
      listQuery: {
        serviceName: undefined,
        status: undefined,
        serviceType: undefined,
        page: 1,
        limit: this.$store.getters.defaultPageSize,
      },
    }
  },
  computed: {
    ids() {
      const ids = []
      this.multipleSelection.forEach((ele) => {
        ids.push(ele.id)
      })
      return ids.join(',')
    },
  },
  created() {
    this.getList()
  },
  methods: {
    //开始终止
    handleChangeJobStatus(row, cmd, cmdLabel) {
      this.$confirm('你确定要' + cmdLabel + '该计划任务么?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        let obj = {
          jobName: row.serviceName,
          id: row.id,
          checkStatus: cmd,
        }
        runJobNow(obj).then((response) => {
          const data = response.data
          if (data.code === 0) {
            this.$message.success(data.msg)
            this.getList()
          } else {
            this.$message.error(data.errmsg)
          }
        })
      })
    },
    runJobNow(row) {
      console.log('定时执行')
      runJobNow({ id: row.id }).then((res) => {
        if (res.data.code == 0) {
          this.$message.success(res.data.msg)
          this.getList()
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    getList() {
      this.listLoading = true
      fetchMarketServiceIntegrationList(this.listQuery).then((response) => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleInfo(row) {
      this.$refs.info.handleInfo(row.id)
    },
    handleCreate() {
      this.$refs.form.handleCreate()
    },
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row.id)
    },
    handleDelete(row) {
      this.$confirm('确定删除该数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteMarketServiceIntegration(row.id).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleBathDelete() {
      if (this.ids === '') {
        this.$message.warning('请选择至少一条数据')
        return
      }
      this.$confirm('确定删除所选数据吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        deleteMarketServiceIntegration(this.ids).then((response) => {
          if (response.data.code === 0) {
            this.getList()
            this.$message.success(response.data.msg)
          } else {
            this.$message.error(response.data.msg)
          }
        })
      })
    },
    handleImport() {
      this.$refs.import.show()
    },
    handleExport() {
      this.downloadLoading = true
      exportMarketServiceIntegration(this.listQuery).then((response) => {
        this.downloadLoading = true
        if (response.data.code === 0) {
          import('@/vendor/Export2Excel').then((excel) => {
            excel.export_byte_to_excel(response.data.data.bytes, response.data.data.title)
            this.downloadLoading = false
          })
        } else {
          this.$message.error(response.data.msg)
        }
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
  },
}
</script>
<style lang="scss" scoped>
::v-deep(::-webkit-scrollbar) {
  display: block !important;
}
.table-expand {
  font-size: 0;
}
.table-expand label {
  width: 90px;
  color: #99a9bf;
}
.table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
.span {
  color: #606266;
  font-weight: 700;
  font-size: 14px;
}
</style>
